package com.tthdhm.truyvetduongdi.util;

import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * class connection.
 * @author user
 */
public class Connection {

    /**
     *	@type Properties which used to get resource
     */
    private static Properties props = new Properties();

    /**
     * @load the content of db.properties file.
     * @first way: read db.properties file as input stream and load into
     * @Properties object.
     */
    static {
        try {
            InputStream is = Connection.class.getResourceAsStream("/db.properties");
            props.load(is);
        } catch (Exception ex) {
            // in case occur exception, log it.
            LogSystem.getInstance().
                    error("Load db.properties:" + ex);
        }
    }
    /**
     * @second way: using ResourceBundle class: fast and easy
     */
    private static ResourceBundle dbResource = ResourceBundle.getBundle("db");

    /**
     * @return the Connection for connect database
     */
    public static java.sql.Connection getConnection() {
        // load the driver and establish a connection.
        try {
            // using resource bundle to get jdbcDriver
            String jdbcDriver = dbResource.getString(Constants.JDBD_DRIVER);
            String jdbcUrl = dbResource.getString(Constants.JDBD_URL);
            props.setProperty("characterEncoding", "utf8");

            // using properties object.
            String jdbcUser = props.getProperty(Constants.JDBD_USER);
            String jdbcPassword = props.getProperty(Constants.JDBD_PASS);

            // Set properties object
            props.setProperty("user", jdbcUser);
            props.setProperty("password", jdbcPassword);
          

            Class.forName(jdbcDriver);
            return (java.sql.Connection) DriverManager.getConnection(jdbcUrl,
                    props);
        } catch (Exception ex) {
            // in case occur exception, log it.
            LogSystem.getInstance().error("getConnection:" + ex);
        }
        return null;
    }
    
    public static String layPublicKeyRecapcha(){
        return dbResource.getString(Constants.PUBLIC_KEY);
    }
    
     public static String layPrivateKeyRecapcha(){
        return dbResource.getString(Constants.PRIVATE_KEY);
    }
}
